// 游学
layui.use(['jquery', 'element', 'table', 'layer', 'form', 'upload'], function(){
    var $ = layui.$;
    var productOrderTable = layui.table;
    var ctxPath = "/education";

    productOrderTable.render({
        id: 'productOrderTableId',
        elem: '#productOrderTableId',
        url: ctxPath + '/api/order/list',
        page: true,
        method: 'post',
        cols:[[
            {field:'id', title:'ID', width: 50},
            {field:'orderCode', title:'订单号', width: 200},
            {field:'productType', title:'产品类型', width: 100,templet: function(d){
                    if ("0" == d.productType) {
                        return '<span >游学</span>';
                    } else if ("1" == d.productType) {
                        return '<span >留学</span>';
                    }
                }
            },
            {field:'productName', title:'产品名称', width: 200 },
            {field:'productCode', title:'产品编号', width: 100 },
            {field:'groupName', title:'团名', width: 150 },
            {field:'applicantName', title:'申请人', width: 150 },
            {field:'orderStatus', title:'状态', width: 150 ,templet: function(d){
                 if ("UN_PAYED" == d.orderStatus) {
                    return '<span >未付款</span>';
                 } else if ("PAYED" == d.orderStatus) {
                    return '<span >已付款</span>';
                 } else if ("ORDERED" == d.orderStatus) {
                    return '<span >已报名</span>';
                 } else if ("GOODS_SENT" == d.orderStatus) {
                    return '<span >已发货</span>';
                 } else if ("CLOSED" == d.orderStatus) {
                    return '<span >已关闭</span>';
                 } else if ("COMPLETE" == d.orderStatus) {
                    return '<span >已完成</span>';
                 }
            }},
            {field:'address', title:'地址', width: 100 },
            {fixed:'right', title:'操作', width: 200, align:'center', toolbar: '#productOrderBar'}
        ]],
        response: {
            statusName: 'returnCode' //数据状态的字段名称，默认：code
            ,statusCode: 0 //成功的状态码，默认：0
            ,msgName: 'returnMsg' //状态信息的字段名称，默认：msg
            ,countName: 'count' //数据总数的字段名称，默认：count
            ,dataName: 'data' //数据列表的字段名称，默认：data
        },
        done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);

            //得到当前页码
            console.log(curr);

            //得到数据总量
            console.log(count);
        }
    });

    var active = {
        searchStudyToursProduct: function(){
            var productName = $('#productName').val();
            var phone = $('#phone').val();
            var groupName = $('#groupName').val();
            var status = $('#orderStatus').val();
            var productType = $('#productType').val();
            productOrderTable.reload('productOrderTableId', {
                where: {
                    productName: productName,
                    phone: phone,
                    groupName: groupName,
                    orderStatus: status,
                    productType: productType
                }
            });
        },
        cancelBtn: function(e) {
            window.location.href = ctxPath + '/html/productOrder/productOrderList.html';
        }
    };

    //按钮事件
    $('.layui-btn').on('click', function(e){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    productOrderTable.on('tool(productOrder)', function(obj){
        var data = obj.data ,layEvent = obj.event;
        if(layEvent === 'detail'){
            location.href = ctxPath + "/html/productOrder/viewProductOrder.html?id=" + data.id;
        } else if(layEvent === 'delete'){
            layer.confirm('确定关闭订单？', function(){
                //向服务端发送指令
                $.ajax({
                    url: ctxPath + '/api/order/close',
                    data: {'id' : data.id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if(result.code == 0) {
                            layer.alert('设置成功！', function(){
                                location.href = ctxPath + "/html/productOrder/productOrderList.html";
                            });
                        } else {
                            layer.alert("设置失败：" + result.msg);
                        }
                    }
                });
            });
        } else if(layEvent === 'complete') {
            layer.confirm('确认将订单设为已完成', function () {
                //向服务端发送指令
                $.ajax({
                    url: ctxPath + '/api/order/complete',
                    data: {'id': data.id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 0) {
                            layer.alert('设置成功！', function () {
                                location.href = ctxPath + "/html/productOrder/productOrderList.html";
                            });
                        } else {
                            layer.alert("设置失败：" + result.msg);
                        }
                    }
                });
            });
        }else if(layEvent === 'payAll') {
            layer.confirm('确认将订单设为已付尾款', function () {
                //向服务端发送关闭指令
                $.ajax({
                    url: ctxPath + '/api/order/payAll',
                    data: {'id': data.id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == 0) {
                            layer.alert('设置成功！', function () {
                                location.href = ctxPath + "/html/productOrder/productOrderList.html";
                            });
                        } else {
                            layer.alert("设置失败：" + result.msg);
                        }
                    }
                });
            });
        }else if(layEvent === 'edit'){
            location.href = ctxPath + "/html/productOrder/formProductOrder.html?id=" + data.id;
        }
        return false;
    });


});
